/**
 * @project name:mail
 */
package com.situ.mail.shop.controller;

import com.situ.mail.base.BaseController;
import com.situ.mail.shop.pojo.User;
import com.situ.mail.shop.service.UserService;
import com.situ.mail.utils.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @Author : yun&bo
 * @CreateDate : ==>2020/12/24 9:04
 * @ClassName : ==>
 * @Description : ==>
 * @Version
 */

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    private static final String SHOP_LOGIN = "shop/login";
    private UserService userService;
    private static final String USER_PAGERS = "pagers";
    private static final String USER_LIST = "user/user_list";
    private static final String USER_PAGERS_ORDER_BY_NAME = "user/user_list";
    private static final String USER_LIST_SON = "user/user_list_son";
    private static final String USER_LIST_GO = "user/findBySql";
    private static final String USER_ADD_LIST = "user/user_add_list";
    private static final String USER_ADD_SON_LIST = "user/user_add_son_list";
    private static final String USER_EDIT_LIST = "user/user_edit";


    public UserController(UserService userService) {
        this.userService = userService;
    }


    /**
     * 得到所有用户
     */
    @RequestMapping("/findBySql")
    public String findBySql(Model model, User user) {
        System.out.println(user);
        Pager<User> pager = userService.findByEntity(user);
        model.addAttribute(USER_PAGERS, pager);
        model.addAttribute(user);
        return USER_LIST;
    }


    @RequestMapping("/delete")
    public String delete(Long rowId) {
        userService.deleteById(rowId);
        return "redirect:/" + USER_LIST_GO;
    }

    /**
     * 注册
     */
    @RequestMapping("/doregister")
    public Integer doRegister(User user) {
        System.out.println("doregisterUserController" + user);
        Integer integer = userService.doAddUser(user);
        return integer;
    }

    @ResponseBody
    @RequestMapping("/edit")
    public ModelAndView editUser(ModelAndView modelAndView, User user) {
        user = userService.getById(user.getRowId());
        modelAndView.addObject("data", user);
        modelAndView.setViewName(USER_EDIT_LIST);
        return modelAndView;
    }

    @RequestMapping("/doedit")
    public String doEdit(User user) {
        userService.updateById(user);
        return "redirect:/" + USER_LIST_GO;

    }

    @RequestMapping("/checkUser")
    public Integer checkUser(String userCode) {
        Integer integer = userService.findUserByCode(userCode);
        return integer;
    }

    /**
     * 登录功能
     * 返回 1 成功
     * 返回 2 失败
     *
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/exitLogin")
    public ModelAndView doExitLogin(ModelAndView modelAndView, HttpServletResponse response, HttpServletRequest request) {
        Integer integer = userService.exitLogin(request, response);
        modelAndView.setViewName(SHOP_LOGIN);
        return modelAndView;
    }


}
